package com.cjy.controller;


import com.cjy.pojo.Books;
import com.cjy.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BookController {
    @Autowired()
    @Qualifier("bookServiceImpl")
    private bookService bookService;
    @RequestMapping("/queryAll")
    public String queryAll(Model model){

        List<Books> list=bookService.queryAllBook();
        model.addAttribute("list", list);

        return "book_list_11";
    }

    @RequestMapping("/addBook")
    public  String addBook(String b_isbn,Books books,Model model)
    {
        System.out.println("竟来l");
        System.out.println(b_isbn);
        List<Books> books1 = bookService.queryBookByIsbn(b_isbn);
        if (!books1.isEmpty()){
            model.addAttribute("errorAddBook","ISBN号重复添加失败");
            return "book_add_11";
        }
        bookService.addBook(books);
        List<Books> list=bookService.queryAllBook();
        model.addAttribute("list", list);
        return "book_list_11";
    }
    @RequestMapping("/deleteBook")
    public String deleteBook(String b_isbn,Model model)
    {
        List<Books> books1 = bookService.queryBookByIsbn(b_isbn);
        if (books1.isEmpty()){
            model.addAttribute("deleteBook","未找到Isbn删除失败");
            return "book_del_11";
        }
        bookService.deleteBookByIsbn(b_isbn);
        List<Books> list=bookService.queryAllBook();
        model.addAttribute("list", list);
        return "book_list_11";
    }
    @RequestMapping("/capacityBook/{bookId}")
    public String capacityBook(@PathVariable("bookId") int id,Model model)
    {
        Books books = bookService.queryBookById(id);
        model.addAttribute("book",books);

        return "book_capacity_11";
    }






}
